package clients;

/*
This file is part of Open Grid Job Management System (OGJMS).

OGJMS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

OGJMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OGJMS.  If not, see <http://www.gnu.org/licenses/>.

The entire and newest source code can be found at http://code.google.com/p/ogjms/  
*/

import grid.stefan.namespaces.jobManagement.FactoryService.CreateResource;
import grid.stefan.namespaces.jobManagement.FactoryService.CreateResourceResponse;
import grid.stefan.namespaces.jobManagement.FactoryService.FactoryPortType;
import grid.stefan.namespaces.jobManagement.FactoryService.service.FactoryServiceAddressingLocator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import jobManagement.impl.JobManagementQNames;

import org.apache.axis.client.Stub;
import org.globus.axis.message.addressing.Address;
import org.globus.axis.message.addressing.EndpointReferenceType;
import org.globus.axis.util.Util;
import org.globus.wsrf.encoding.ObjectSerializer;
import org.globus.wsrf.impl.security.authentication.Constants;
import org.globus.wsrf.impl.security.authorization.HostAuthorization;
import org.globus.wsrf.impl.security.authorization.NoAuthorization;
import org.globus.wsrf.impl.security.authorization.SelfAuthorization;
import org.globus.wsrf.impl.security.descriptor.ClientSecurityDescriptor;

import JobManagementUtils.Utils;

public class FactoryServiceProxyClass {

	static {
		Util.registerTransport();
	}

	public void createResource(String fileName) {
		try {

			Utils.createProxy(Optiuni.getProxyFile(),
					Optiuni.getCertFile(), Optiuni.getKeyFile(), Optiuni
							.getPassword());

			FactoryServiceAddressingLocator factoryLocator = new FactoryServiceAddressingLocator();

			String eprFilename = fileName + ".epr";

			EndpointReferenceType factoryEPR, instanceEPR;
			FactoryPortType jobManagementFactory;

			// Get factory portType
			factoryEPR = new EndpointReferenceType();

			factoryEPR.setAddress(new Address(Optiuni.getFactoryURL()));

			jobManagementFactory = factoryLocator
					.getFactoryPortTypePort(factoryEPR);
			setSecurity((Stub) jobManagementFactory);

			// Create resource and get endpoint reference of
			// WS-Resource
			CreateResourceResponse createResponse = jobManagementFactory
					.createResource(new CreateResource());
			instanceEPR = createResponse.getEndpointReference();

			// Write endpoint reference to file

			String endpointString = ObjectSerializer.toString(instanceEPR,
					JobManagementQNames.RESOURCE_REFERENCE);
			System.out.println(eprFilename);
			File fileItem = new File(eprFilename);
			if (fileItem.exists())
				return;

			fileItem.createNewFile();
			FileWriter fileWriter = new FileWriter(eprFilename);
			BufferedWriter bfWriter = new BufferedWriter(fileWriter);
			bfWriter.write(endpointString);
			bfWriter.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void setSecurity(Stub stub) {
		String CLIENT_DESC = "clients/client-security-descriptor.xml";
		ClientSecurityDescriptor desc = new ClientSecurityDescriptor();
		desc.setAuthz(new NoAuthorization());
		//desc.setSecConvAnonymous();
		desc.setGSISecureConv(Constants.SIGNATURE);
		((Stub) stub)._setProperty(Constants.CLIENT_DESCRIPTOR, desc);

	}

}
